package com.sunwayworld.basemodule.rm.filemanagement.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sunwayworld.basemodule.rm.filemanagement.bean.LimsFilemanagementVersionBean;
import com.sunwayworld.basemodule.rm.filemanagement.service.LimsFilemanagementVersionService;
import com.sunwayworld.basemodule.rm.person.bean.LimsPersonAbilityBean;
import com.sunwayworld.basemodule.rm.person.bean.LimsPersonBean;
import com.sunwayworld.basemodule.rm.person.bean.LimsPersonCertificateBean;
import com.sunwayworld.framework.audit.aunnotation.Audit;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.support.PersistableMetadataHelper;
import com.sunwayworld.framework.utils.StringUtils;
import com.sunwayworld.module.sys.metadata.service.CoreTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.rm.filemanagement.dao.LimsFilemanagementDao;
import com.sunwayworld.basemodule.rm.filemanagement.bean.LimsFilemanagementBean;
import com.sunwayworld.basemodule.rm.filemanagement.service.LimsFilemanagementService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsFilemanagementServiceImpl implements LimsFilemanagementService {

    @Autowired
    private LimsFilemanagementDao limsFilemanagementDao;

    @Autowired
    private LimsFilemanagementVersionService limsFilemanagementVersionService;

    @Override
    @SuppressWarnings("unchecked")
    public LimsFilemanagementDao getDao() {
        return limsFilemanagementDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsFilemanagementBean limsFilemanagement = jsonWrapper.parseUnique(LimsFilemanagementBean.class);
        limsFilemanagement.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsFilemanagement);
        return limsFilemanagement.getId();
    }

    @Override
    public JSONObject updatePerson(RestJsonWrapperBean wrapper) {
        JSONObject req = new JSONObject();
        List<LimsFilemanagementBean> files = wrapper.parse(LimsFilemanagementBean.class);
        String person = wrapper.getParamValue("person");
        String personId = wrapper.getParamValue("personId");
        if (files.isEmpty() || StringUtils.isBlank(person) || StringUtils.isBlank(personId)) {
            return req;
        }
        List<Long> ids = files.stream().map(LimsFilemanagementBean::getId).filter(Objects::nonNull).collect(Collectors.toList());
        LimsFilemanagementBean update = new LimsFilemanagementBean();
        update.setPerson(person);
        update.setPersonId(personId);

        getDao().updateByIds(update, ids, "person", "personId");
        return req;
    }

    @Transactional
    @AuditTrailEntry(AuditTrailType.DELETE)
    @Audit("AUDIT.DELETE")
    public void delete(RestJsonWrapperBean wrapper) {
        List<LimsFilemanagementBean> deleteList = wrapper.parse(this.getDao().getType());
        if (!deleteList.isEmpty()) {

            List<Long> ids = deleteList.stream().map(LimsFilemanagementBean::getId).collect(Collectors.toList());
            List<LimsFilemanagementVersionBean> versionBeans = limsFilemanagementVersionService.selectListByFilter(SearchFilter.instance().match("fileId", ids).filter(MatchPattern.OR));
            if (!versionBeans.isEmpty()) {
                limsFilemanagementVersionService.getDao().deleteByIdList(versionBeans.stream().map(LimsFilemanagementVersionBean::getId).collect(Collectors.toList()));
            }

            Class<LimsFilemanagementBean> itemType = this.getDao().getType();
            CoreTableService tableService = (CoreTableService) ApplicationContextHelper.getBean(CoreTableService.class);
            deleteList.forEach((t) -> {
                tableService.deleteCascade(PersistableMetadataHelper.getTableName(itemType), (Serializable) t.getId());
            });
        }
    }
}
